from kafka import KafkaConsumer

# 1、创建消费者
consumers = KafkaConsumer(
    "students_partition_age",  # 消费的topic
    bootstrap_servers="master:9092",  # kafka集群地址
    group_id="group1`",  # 消费者组，同一条数据在一个组内只消费一次
    auto_offset_reset="earliest",  # earliest：从最早的消息开始消费，latest：从最新的消息开始消费
    enable_auto_commit=True,  # 自动提交offset,消费便量保存在__consumer_offsets  topic中
)

for line in consumers:
    value = line.value.decode("utf-8")
    key = line.key
    partition = line.partition
    offset = line.offset
    timestamp = line.timestamp
    print("value:", value, "key:", key, "partition:", partition, "offset:", offset, "timestamp:", timestamp)